/**
 * 资源管理器
 */
class AssetsManager {

    /**等待加载队列*/
    private static map: Array<any> = [];
    /**是否可加载*/
    private static isLoad: boolean = true;
    /** 本次加加载资源组 */
    private static assetsSourceArr: string[] = [];
    /** 当前加载组index */
    private static index: number = 0;
    /** 本次加载名称 */
    private static sname: string = "";
    /** 本次加载携带数据 */
    private static data: any;
    /** 加载资源成功后的回调方法 */
    private static callback: Function;

    /**
     * 加载资源
     *  name: string  类型
     *  assetsSourceArr: string[]  要加载的资源数组名称
     *  callback: Function   加载完成后的回调方法
     */
    public static loadGroup(name: string, assetsSourceArr: string[], callback: Function, data?: any): void {
        if (!AssetsManager.isLoad) {
            var data: any = {};
            data["name"] = name;
            data["assetsSourceArr"] = assetsSourceArr;
            data["callback"] = callback;
            data["data"] = data;
            AssetsManager.map.push(data);
            return;
        }

        AssetsManager.isLoad = false;

        AssetsManager.sname = name;
        AssetsManager.callback = callback;
        AssetsManager.data = data;

        var b: boolean = false;
        var len: number = assetsSourceArr.length;

        AssetsManager.assetsSourceArr.length = 0;
        for (var i: number = 0; i < len; i++) {
            if (!RES.isGroupLoaded(assetsSourceArr[i])) {//检查某个资源组是否已加载完成
                AssetsManager.assetsSourceArr.push(assetsSourceArr[i]);
                b = true;
            }
        }

        if (b) {
            RES.addEventListener(RES.ResourceEvent.GROUP_COMPLETE, this.onResourceLoadComplete, this);
            RES.addEventListener(RES.ResourceEvent.GROUP_LOAD_ERROR, this.onResourceLoadError, this);
            RES.addEventListener(RES.ResourceEvent.GROUP_PROGRESS, this.onResourceProgress, this);
            AssetsManager.index = 0;

            RES.loadGroup(AssetsManager.assetsSourceArr[AssetsManager.index]);
        } else {
            AssetsManager.callback(AssetsManager.data);
            AssetsManager.assetsSourceArr.length = 0;
            AssetsManager.isLoad = true;
            if (AssetsManager.map.length) {
                var temp: any = AssetsManager.map.shift();
                this.loadGroup(temp["name"], temp["assetsSourceArr"], temp["callback"], temp["data"]);
            }
        }
    }
    /**
     * preload资源组加载完成
     * preload resource group is loaded
     */
    private static onResourceLoadComplete(event: RES.ResourceEvent): void {
        //if (event.groupName == this.assetsSourceArr[this.index]) {
        egret.log("LoadComplete:" + event.groupName + " ");
        if (AssetsManager.assetsSourceArr.length > AssetsManager.index + 1) {
            AssetsManager.index++;
            RES.loadGroup(AssetsManager.assetsSourceArr[AssetsManager.index]);
        } else {
            RES.removeEventListener(RES.ResourceEvent.GROUP_COMPLETE, this.onResourceLoadComplete, this);
            RES.removeEventListener(RES.ResourceEvent.GROUP_LOAD_ERROR, this.onResourceLoadError, this);
            RES.removeEventListener(RES.ResourceEvent.GROUP_PROGRESS, this.onResourceProgress, this);
            AssetsManager.callback(AssetsManager.data);
            AssetsManager.isLoad = true;
            if (AssetsManager.map.length) {
                var temp: any = AssetsManager.map.shift();
                this.loadGroup(temp["name"], temp["assetsSourceArr"], temp["callback"], temp["data"]);
            }
        }
        //}
    }
    /**
     * 资源组加载出错
     * Resource group loading failed
     */
    private static onResourceLoadError(event: RES.ResourceEvent): void {
        //TODO
        egret.warn("LoadError:" + event.groupName + " has failed to load");
        //TextEffect.showArrText(["LoadError:" + event.groupName + " has failed to load"]);
        //忽略加载失败的项目
        //ignore loading failed projects
        this.onResourceLoadComplete(event);
    }
    /**
     * preload资源组加载进度
     * loading process of preload resource
     */
    private static onResourceProgress(event: RES.ResourceEvent): void {
        let gName = AssetsManager.assetsSourceArr[AssetsManager.index];
        if (event.groupName == gName) {
            let p: Number = event.itemsLoaded / event.itemsTotal * (100 / AssetsManager.assetsSourceArr.length)
                + AssetsManager.index * (100 / AssetsManager.assetsSourceArr.length);
            // var data: any = { progress: event.itemsLoaded, itemsTotal: event.itemsTotal};
            egret.log(this.sname + "  current====" + p.toFixed(0));
            let param = { sname: AssetsManager.sname, name: gName, progress: p.toFixed(0) };
            EventManager.dispatch(EventData.RES_LOAD_PROGRESS, param);
        }
    }
}
